package edu.gdkm.sql.dao;

import edu.gdkm.sql.pojo.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    // 查询全部类别
    @Select("select * from product")
    @Results(id = "productMap",value = {
            @Result(property = "typeid",column = "typeid"),
            @Result(property = "category", column = "typeid",
                    one = @One(select = "edu.gdkm.sql.dao.CategoryMapper.findById")),
            @Result(property = "id", column = "id"),
            @Result(property = "productdetails", column = "id",
                    many = @Many(select = "edu.gdkm.sql.dao.ProductdetailMapper.findProductById"))
    })
    List<Product> findAll();
    // 根据id查询类别
    @Select("SELECT * FROM product WHERE id = #{id}")
    Product findById(int id);


    @Insert("INSERT INTO product(goodsname,price,typeid) VALUES(#{goodsname},#{price},#{typeid}})")
    int insertProduct(Product product);

    // 更新类别
    @Update("UPDATE product SET goodsname=#{goodsname},price=#{price},typeid=#{typeid} WHERE id=#{id}")
    int updateProductById(Product product);

    // 删除类别
    @Delete("DELETE FROM product WHERE id=#{id}")
    int deleteProductById(int id);



    @Select("select * from product where typeid = #{typeid}")
    @ResultMap("productMap")
    List<Product> findByTypeId();

    @Select("select * from product where goodsname = #{goodsname}")
    Product findByName(String goodsname);
}
